package cn.kgc.layui.service.impl;

import cn.kgc.layui.dao.UserDao;
import cn.kgc.layui.dao.impl.UserDaoImpl;
import cn.kgc.layui.entity.User;
import cn.kgc.layui.service.UserService;
import cn.kgc.layui.utils.CodeMsg;
import cn.kgc.layui.utils.LayResult;

import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/9/15
 */
public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDaoImpl();


    @Override
    public LayResult selectPage(Integer page, Integer limit, User user) {

        Integer start = (page-1)*limit;
        // 查询分页数据
        List<User> users =  userDao.selectPage(start,limit,user);
        // 查询数据总条数
        Long count = userDao.selectTotal(user);
        // 将以上数据封装成LayResult 对象返回
        return new LayResult(CodeMsg.SELECT_SUCCESS.getCode(), CodeMsg.SELECT_SUCCESS.getMsg(), count, users);
    }

    // 添加用户信息
    @Override
    public LayResult add(User usr) {

        Integer line =   userDao.add(usr);
        if(line>0){
            return  new LayResult(CodeMsg.ADD_SUCCESS.getCode(), CodeMsg.ADD_SUCCESS.getMsg());
        }else {
            return  new LayResult(CodeMsg.OPERATE_FAILED.getCode(), CodeMsg.OPERATE_FAILED.getMsg());
        }

    }

    @Override
    public LayResult update(User usr) {
        Integer line = userDao.update(usr);
        if(line>0){
            return  new LayResult(CodeMsg.UPDATE_SUCCESS.getCode(), CodeMsg.UPDATE_SUCCESS.getMsg());
        }else {
            return  new LayResult(CodeMsg.OPERATE_FAILED.getCode(), CodeMsg.OPERATE_FAILED.getMsg());
        }
    }

    @Override
    public LayResult delete(String id) {
        Integer line = userDao.delete(id);
        if(line>0){
            return  new LayResult(CodeMsg.DELETE_SUCCESS.getCode(), CodeMsg.DELETE_SUCCESS.getMsg());
        }else {
            return  new LayResult(CodeMsg.OPERATE_FAILED.getCode(), CodeMsg.OPERATE_FAILED.getMsg());
        }
    }

    @Override
    public LayResult deleteMore(String[] idsArray) {

        //遍历数组  删除用户信息
        try {
            for (String id : idsArray) {
                userDao.delete(id);
            }
            return  new LayResult(CodeMsg.DELETE_SUCCESS.getCode(), CodeMsg.DELETE_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();

        }
        return new LayResult(CodeMsg.OPERATE_FAILED.getCode(), CodeMsg.OPERATE_FAILED.getMsg());
    }
}
